<?php
// +----------------------------------------------------------------------
// | likeshop开源商城系统
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | gitee下载：https://gitee.com/likeshop_gitee
// | github下载：https://github.com/likeshop-github
// | 访问官网：https://www.likeshop.cn
// | 访问社区：https://home.likeshop.cn
// | 访问手册：http://doc.likeshop.cn
// | 微信公众号：likeshop技术社区
// | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用，未经许可不能去除前后端官方版权标识
// |  likeshop系列产品收费版本务必购买商业授权，购买去版权授权后，方可去除前后端官方版权标识
// | 禁止对系统程序代码以任何目的，任何形式的再发布
// | likeshop团队版权所有并拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeshop.cn.team
// +----------------------------------------------------------------------
namespace app\admin\logic;
use app\admin\model\Driver;
use app\admin\model\User;
use app\common\logic\AccountLogLogic;
use app\common\logic\UserLevelLogic;
use app\common\model\AccountLog;
use app\common\model\Order;
use app\common\server\UrlServer;
use think\Db;
use think\Exception;

class DriverLogic{
    /**
     * 列表
     * @param $get
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function lists($get){
        $user = new Driver();

        $where = [];
//        $where[] = ['del','=', '0'];

        
        if(isset($get['dmobile']) && $get['dmobile'] != ''){
            $where[] = ['dmobile','like',$get['dmobile']."%"];
        }


        if(isset($get['dname']) && $get['dname']){
            $where[] = ['dname','like',$get['dname']."%"];
        }

        if(isset($get['dtype']) && $get['dtype']){
            $where[] = ['dtype','=',$get['dtype']];
        }


        //开户时间
        if(isset($get['start_time']) && $get['start_time']!=''){
            $where[] = ['create_time','>=',$get['start_time']];
        }
        if(isset($get['end_time']) && $get['end_time']!=''){
            $where[] = ['create_time','<=',$get['end_time']];
        }

        $user_count = $user
                    ->where($where)
                    ->count();

        $user_list = $user
                    ->where($where)
                    ->page($get['page'],$get['limit'])
                    ->order('id desc')
                    ->select()->toArray();
//        var_dump($user_list);
        return ['count'=>$user_count , 'lists'=>$user_list];
    }


    /**
     * Notes: 获取会员信息
     * @param $id
     * @param bool $getdata 是否获取原始数据
     * @param bool $expenditure 是否显示会员消费
     * @return User|mixed
     */
    public static function getUser($id,$getdata = false){
        $user =  Driver::get($id);
        return $user;
    }

    //新增用户
    public static function add($post){
        $user = new Driver();
        //拼接数据
        $data = $post;
        $data['davatar'] = UrlServer::getFileUrl('/static/common/image/default/user2.png');
        $data['create_time' ] = date("Y-m-d H:i:s",time());
        return $user->allowField(true)->save($data);
    }

    /*
     * 编辑会员
     */
    public static function edit($post){
        $user = Driver::get($post['id']);
        $res = $user->allowField(true)->save($post);
//        echo $user->getLastSql();
        return $res;
    }

    public static function getList($get){
        switch ($get['type']){
            case 0://下单明细
                $order = new Order();
                $count = $order->alias('o')
                        ->where(['user_id'=>$get['user_id']])
//                        ->where('status','not in',[Order::STATUS_WAIT_PAY])
                        ->count();

                $list = $order->alias('o')
                        ->where(['user_id'=>$get['user_id']])
//                        ->where('status','not in',[Order::STATUS_CLOSE,Order::STATUS_WAIT_PAY])
                        ->page($get['page'],$get['limit'])
                        ->order('id desc')
                        ->field('id,order_sn,bottle_num,pay_time,order_amount,bottle_amount,
                                create_time,consignee,o.address,mobile')
                        ->select();
                break;

        }

        return ['count'=>$count , 'lists'=>$list];
    }
}